package com.ocom.payhandle.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ocom.common.entity.company.EntityCompanyConfig;
import com.ocom.common.enums.DataSourceEnum;
import com.ocom.common.enums.MasterSlaveEnum;
import com.ocom.mysql.aspect.DataSourceAspect;
import com.ocom.payhandle.dao.IDaoCompanyConfig;
import com.ocom.payhandle.service.CompanyConfigService;
import com.ocom.redis.util.RedisUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;


@Service
public class CompanyConfigServiceImpl extends ServiceImpl<IDaoCompanyConfig, EntityCompanyConfig> implements CompanyConfigService {


    @Resource
    private RedisUtil redisUtil;

    @Override
    @DataSourceAspect(type = DataSourceEnum.PART_DB, isMaster = MasterSlaveEnum.SLAVE)
    public EntityCompanyConfig getTapValue(Long companyId, String key) {
        String canKey = "company_config#comId:" + companyId + "#key:" + key;
        Object requestRedis = redisUtil.get(canKey);
        if (requestRedis == null) {
            Long timeLong = Long.valueOf(4 * 60 * 60);
            QueryWrapper<EntityCompanyConfig> queryWrapper = Wrappers.query();
            queryWrapper.eq("com_id", companyId);
            queryWrapper.eq("tab", key);
            queryWrapper.orderByDesc("update_time");
            queryWrapper.last("limit 1");
            EntityCompanyConfig entityCompanyConfig = this.getOne(queryWrapper);
            if (entityCompanyConfig == null) {
                redisUtil.set(canKey, "null", timeLong);
            } else {
                redisUtil.set(canKey, entityCompanyConfig, timeLong);
            }
            return entityCompanyConfig;
        } else {
            if (requestRedis.toString().equals("null")) {
                return null;
            }
            return (EntityCompanyConfig) requestRedis;
        }


    }


}
